/**
 *
 */
var app = angular.module('zhijiaoAdminApp.controllers');

app.controller('AccessListCtr' , ['$rootScope','$scope','ajaxService' ,'toastPCService','layerService',
function($rootScope , $scope , ajax ,toast , layer){

	var editInit = function(){
		return {
			name : '',
			pid  : 0,
			icon : '',
			url  : '',
			key  : '',
			menu : 1
		};
	}

	$scope.edit = {};

	$scope.data = {
			list  : [],
			pages : 1,
			count : 0,
			parentMenu : [],
			editParentMenu : []
	};

	$scope.search = {
			pid   : '',
			key   : '',
			type  : '1',
			page  : 1,
			pageSize : 20,
	};


	$scope.getList = function(){
		ajax('access_list', $scope.search , function(rel){
			if(!rel.code){
				$scope.data.list = rel.data.list.data;
				$scope.data.pages= rel.data.list.last_page;
				$scope.data.count= rel.data.list.total;

				$scope.data.parentMenu = rel.data.parent_menu;
				$scope.data.editParentMenu = angular.copy(rel.data.parent_menu);
				$scope.data.editParentMenu.unshift({id:0 , name : '空'});
			}
		})
	}

	$scope.showEdit = function(index){
		if(index){
			var item = $scope.data.list[index-1];
			$scope.edit = angular.copy(item);
			console.log($scope.data.parentMenu)
		}else{
			$scope.edit = editInit();
			console.log($scope.edit)
		}
		$scope.edit.type = $scope.search.type;
		$('#myModal').modal("show");
	}


	$scope.commit = function(){
		if(angular.element("#editAccessForm").valid()){
			ajax('access_save', $scope.edit , function(rel){
				if(!rel.code){
					$("#myModal").modal('hide');
					$scope.getList();
				}else{
					toast.show(2 , rel.message);
				}
			})
		}
	}

	$scope.$watch('edit.url' , function(url){
		if(!$scope.edit.id){
			var str = '';
			if(url){
				var arr = url.split(/[\/|_]/);
				arr.forEach(function(item){
					if(item){
						str += item[0].toUpperCase() + item.slice(1);
					}
				})
			}
			$scope.edit.key = str;
		}
	})

	$scope.del = function(item){
		var child = item.pid == 0 ? '，删除之后子节点也会被删除!' : '';
		layer.confirm('确定删除【'+item.name+'】吗?'+child , '' , function(){
			ajax('access_del' , {id : item.id} , function(rel){
				if(!rel.code){
					$scope.getList();
				}else{
					toast.show(2 , rel.message);
				}
			})
		})
	}

	$scope.$on('$stateChangeSuccess' , function(){
		$scope._form = angular.element("#editAccessForm").validate({
			rules : {
				name : {required : true , maxlength : 30},
				key  : {required : true , maxlength : 50}
			},
			messages : {
				name : {
					required : '请输入节点名',
					maxlength: '不能超过30个字'
				},
				key : {
					required : '请输入key,不能重复',
					maxlength: '不能超过50个字'
				}
			}
		});
	})

}])

app.controller('roleListCtr' , ['$rootScope' , '$scope','ajaxService' , 'toastPCService','layerService','$timeout',
	function($rootScope , $scope , ajax , toast , layer , $timeout){

		$scope.data = {
				tree : [],
				listData: {},
				list : [],
				access : [],
		};

		var editInit = function(){
			return {
				id : '',name : '' ,house_role: '0' , department : '',department_id : '' , status : 1,access : [],person:[]
			};
		}

		$scope.edit = editInit();
		//显示选择部门
		$scope.showSelectDepartment = false;

		$scope.getRoleList = function(){
			ajax('role_list' , {} , function(rel){
				if(!rel.code){
					$scope.data.list = rel.data;
				}
			})
		}

		$scope.getRoleList();

		$scope.showTab = 1;
		$scope.showEdit = function(item){
			var role = angular.copy(item);

			if(role){
				role.access  = role.access.map(function(item){return item.id});
				role.house_role = String(role.house_role);
				$scope.edit = role;
			}else{
				role = {access : []};
				$scope.edit = editInit();
			}
			$scope.edit.role = role;
			if($scope.data.access.length == 0){
				ajax('getAccessTree' , {} , function(rel){
					if(!rel.code){
						$scope.data.accessAll = rel.data;
						$scope.data.access = rel.data;
						$scope.setStatus($scope.data.access , role.access);
					}
				})
			}else{
				$scope.showTab = 1;
				$scope.data.access = $scope.data.accessAll;
				$scope.setStatus($scope.data.access , role.access);
			}

			$("#myModal").modal("show");
		}

		$scope.setStatus = function(allAccess , access){
			var access = access || [];
			allAccess.forEach(function(item){
				item.checked = access.some(function(val){return item.id == val;});
				if(item.children){
					$scope.setStatus(item.children , access);
				}
			})
		}

		$scope.selectDepartment = function(){
			$scope.department = angular.copy($scope.data.tree);
			$scope.showSelectDepartment = !$scope.showSelectDepartment;
		}

		$scope.selectedDepartment = function(item){
			$scope.edit.department    = item.name;
			$scope.edit.department_id = item.id;
		}


		$scope.commit = function(){
			if(angular.element('#editRoleForm').valid()){
				if($scope.edit.access.length == 0){
					toast.show(2 , '请选择权限节点');
				}
				var existIndex = $scope.edit.access.some(function(val){
					return val == 1;
				})
				if(!existIndex){
					$scope.edit.access.push(1);
				}
				var data = editInit();
				for(k in data){
					data[k] = $scope.edit[k];
				}
				ajax('role_save' , data , function(rel){
					if(rel.code){
						toast.show(2 , rel.message);
					}else{
						$scope.getRoleList($scope.currId);
						$('#myModal').modal('hide');
					}
				})
			}
		}


		$scope.del = function(item){
			layer.confirm('确定删除【'+item.name+'】吗?' , '' , function(){
				ajax('role_del' , {id : item.id} , function(rel){
					if(!rel.code){
						$scope.getRoleList($scope.currId);
					}else{
						toast.show(2 , rel.message);
					}
				})
			})
		}


		$scope.showEmployeeModal = false;
		$scope.showEmployee = function(item){
			$scope.showEmployeeModal = true;
			$scope.employeeData = {
					list : [],
					pages : 1,
					count : 0,
					search : {
						page : 1,
						pageSize : 15,
						id : item.id
					}
			};
			setTimeout(function(){
				$('#showEmployee').modal("show");
			},200);
		}

		$scope.getEmployee = function(){
			ajax('role_employee' , $scope.employeeData.search , function(rel){
				$scope.employeeData.list = rel.data.list;
				$scope.employeeData.count = rel.data.count;
				$scope.employeeData.pages = rel.data.pages;
			})
		}

		$scope.closeEmployee = function(){
			$('#showEmployee').modal('hide');
			$timeout(function(){
				$scope.showEmployeeModal = false;
			},200)
		}

		$scope.$on('$stateChangeSuccess' , function(){
	    	$scope._form = angular.element("#editRoleForm").validate({
				rules : {
					name : {required : true , maxlength : 30},
					department : {required : true},
				},
				messages : {
					name : {
						required : '请输入角色名',
						maxlength: '不能超过30个字'
					},
					type : {
						required : '请选择部门',
					}
				}
			});
		})

	}
])
